package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.TestDao;
import com.woniuxy.dao.TestExamDao;
import com.woniuxy.entity.ExamPaper;
import com.woniuxy.dao.ExamPaperDao;
import com.woniuxy.entity.Test;
import com.woniuxy.service.ExamPaperService;
import com.woniuxy.service.TestExamService;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (ExamPaper)表服务实现类
 *
 * @author zjx
 * @since 2021-05-14 19:41:32
 */
@Transactional(rollbackFor = Exception.class)
@Service("examPaperService")
public class ExamPaperServiceImpl implements ExamPaperService {
    @Resource
    private ExamPaperDao examPaperDao;
    @Resource
    private TestExamDao testExamDao;
    @Resource
    private TestDao testDao;
    @Resource
    private TestExamService testExamService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ExamPaper queryById(Integer id) {
        return this.examPaperDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ExamPaper> queryAllByLimit(int offset, int limit) {
        return this.examPaperDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param examPaper 实例对象
     * @return 实例对象
     */
    /*@Transactional(rollbackFor = Exception.class)*/
    @Override
    public int insert(ExamPaper examPaper) {

        return this.examPaperDao.insert(examPaper);
    }

    /**
     * 修改数据
     *
     * @param examPaper 实例对象
     * @return 实例对象
     */
    /*@Transactional(rollbackFor = Exception.class)*/
    @Override
    public int update(ExamPaper examPaper) {

        return this.examPaperDao.update(examPaper);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    /*@Transactional(rollbackFor = Exception.class)*/
    @Override
    public int deleteById(Integer id) {
        return this.examPaperDao.deleteById(id) ;
    }


    @Override
    public PageInfo<Map<String,String>> showExamPaper(int startPage, int pageSize, ExamPaper examPaper) {
        PageHelper.startPage(startPage,pageSize);
        List<Map<String,String>> mapList = examPaperDao.showExamPaper(examPaper);
        System.out.println(mapList);
        PageInfo<Map<String,String>> pageInfo = new PageInfo<>(mapList);
        return pageInfo;
    }

    /**
     *
     * @param    map   试卷基础信息和试题集合
     * @return string
     */
    @Override
    public String addBasicExamPaper(Map<String,Object> map) {
        JSONObject jsonObject=JSONObject.fromObject(map.get("examPaper"));
        ExamPaper examPaper = (ExamPaper)JSONObject.toBean(jsonObject, ExamPaper.class);
        if(examPaper.getShortAnswerTotalScore()!=null&&examPaper.getShortAnswerTotalScore()>0){
            examPaper.setHasShortAnswer(1);
        }
        this.examPaperDao.insert(examPaper);
        int id = examPaper.getId();
        String result = "试卷基础信息插入失败!";
        if(id>=1){
            result = "试卷基础信息插入成功!";
        }


        List<Integer> singleIdList = (List<Integer>) map.get("singleIdList");
        List<Integer> multipleIdList = (List<Integer>) map.get("multipleIdList");
        List<Integer> judgeIdList = (List<Integer>) map.get("judgeIdList");
        List<Integer> fillBlankIdList = (List<Integer>) map.get("fillBlankIdList");
        List<Integer> shortAnswerIdList = (List<Integer>) map.get("shortAnswerIdList");
        List<Integer> list = new ArrayList<>();
        list.addAll(singleIdList);
        list.addAll(multipleIdList);
        list.addAll(judgeIdList);
        list.addAll(fillBlankIdList);
        list.addAll(shortAnswerIdList);
        HashMap<String,Object> map1 = new HashMap<>();
        map1.put("ids",list);
        map1.put("paperId",examPaper.getId());
        int row  = this.testExamDao.insertTestsByPaper(map1);
        if(row>=1){
            result += "插入试卷试题成功";
        }else{
            result += "插入试卷试题失败";
        }
        //result += this.testExamService.addRandomTest(testBankName,testLabel,examPaper);
        return result;
    }

    /**
     * 随机组卷查询
     * @param    map   试题库,试题标签,试题类型,试题数量
     * @return string
     */
    @Override
    @Transactional
    public Map<String, Object> queryIdByTypeBankLabelLimit(Map<String, Object> map) {
        String testBankName = (String) map.get("testBankName");
        String testLabel = (String) map.get("testLabel");
        JSONObject jsonObject=JSONObject.fromObject(map.get("examPaper"));
        ExamPaper examPaper = (ExamPaper)JSONObject.toBean(jsonObject, ExamPaper.class);

        String type = "单选题";
        List<Integer> singleIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,examPaper.getSingleCount(),type);
        System.out.println("单选题题号集合"+singleIdList);

        type = "多选题";
        List<Integer> multipleIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,examPaper.getMultipleCount(),type);
        System.out.println("多选题题号集合"+multipleIdList);

        type = "判断题";
        List<Integer> judgeIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,examPaper.getJudgeCount(),type);
        System.out.println("判断题题号集合"+judgeIdList);

        type = "填空题";
        List<Integer> fillBlankIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,examPaper.getFillBlankCount(),type);
        System.out.println("填空题题号集合"+fillBlankIdList);

        type = "问答题";
        List<Integer> shortAnswerIdList = this.testDao.queryIdByTypeBankLabelLimit(testBankName,testLabel,examPaper.getShortAnswerCount(),type);
        System.out.println("简答题题号集合"+shortAnswerIdList);

        Map<String,Object> result = new HashMap<>();
        result.put("singleIdList",singleIdList);
        result.put("multipleIdList",multipleIdList);
        result.put("judgeIdList",judgeIdList);
        result.put("fillBlankIdList",fillBlankIdList);
        result.put("shortAnswerIdList",shortAnswerIdList);
        return result;
    }

    /**
     * 以试题id集合查试题题干和标签,封装为试题对象集合
     * @param    map   各种类型的试题id集合
     * @return  结果信息    试题对象集合
     */
    @Override
    public List<Test> queryExamPaperTestContent(Map<String, Object> map) {
        List<Integer> singleIdList = (List<Integer>) map.get("singleIdList");
        List<Integer> multipleIdList = (List<Integer>) map.get("multipleIdList");
        List<Integer> judgeIdList = (List<Integer>) map.get("judgeIdList");
        List<Integer> fillBlankIdList = (List<Integer>) map.get("fillBlankIdList");
        List<Integer> shortAnswerIdList = (List<Integer>) map.get("shortAnswerIdList");
        List<Integer> list = new ArrayList<>();
        list.addAll(singleIdList);
        list.addAll(multipleIdList);
        list.addAll(judgeIdList);
        list.addAll(fillBlankIdList);
        list.addAll(shortAnswerIdList);
        List<Test> result = this.testDao.queryBodyLabelById(list);
        return result;
    }

    /**
     *穿梭框查询可选试题集合
     * @param    map   试题库名,试题标签,试题类型,试题数量,试题名称
     * @return 指定类型的可选试题id集合
     */
    @Override
    @Transactional
    public Map<String,Object> queryTestsByTypeLabelBodyBank(Map<String, Object> map) {
        String testBankName = (String) map.get("testBankName");
        String testLabel = (String) map.get("testLabel");
        String testBody = (String) map.get("testBody");
        String type = (String) map.get("type");
        Map<String,Object> returnMap = new HashMap<>();
        List<Integer> result = this.testDao.queryTestsByTypeLabelBodyBank(testBankName,testLabel,testBody,type);
        returnMap.put("OptionalTests",result);
        return returnMap;
    }

    /**
     * 查看试卷
     * @param    id 试卷id
     * @return  试卷对象
     */
    @Override
    public ExamPaper viewTestPaper(int id) {
        ExamPaper examPaper = this.examPaperDao.viewTestPaper(id);
        return examPaper;
    }
    /**
     * 查询所有创建人
     * @return  创建人集合
     */
    @Override
    public List<String> selectCreaters() {
        List<String> creaters =  this.examPaperDao.selectCreaters();
        return creaters;
    }

}
